/**
 * 
 */

let registerVue = new Vue({
	el:'#register',
	data:{
		Email:'',
		mPwduser:{
			uid:'',
			upwd:'',
			checkpwd:''
		},
		divActive:[
			{div:true,index:0},
			{div:false,index:1},
			{div:false,index:2}
		],
		ruleForm:{
			uid:'',
			upwd:'',
			upwd2:'',
			upasswordexpre:'',
			upasswordanswer:'',
			uimg:null,
			ugender:'',
			uinterest:'',
			uemail:'',
			uphone:''
		},
	
		rules:{
			uid:[
				{required:true,message:'请输入用户昵称',trigger:'blur'},
				{min:3,max:20,message:'长度在 3 - 5个字符',trigger:'blur'}
			],
			upwd:[
				{required:true,message:'请输入密码',trigger:'blur'}
			],
			upwd2:[
				{required:true,message:'请输入确认密码',trigger:'blur'}
			],
			ugender:[
				{required:true,message:'请选择性别',trigger:'change'}
			],
			uemail:[
				{required:true,message:'请输入邮箱',trigger:'blur'}
			],
			uphone:[
				{required:true,message:'请输入手机号',trigger:'blur'},
				{max:15,min:3,message:"手机号长度不符",trigger:'blur'}
			]
		},
		percentage:0,
		imageUrl:'',
	    interestList:[]
	
	},
	created(){
		let _this = this;
			axios.post('/usert/interest')
				 .then((response)=>{
					_this.interestList = response.data;
					console.log(response.data);
					
				}).catch(error=>{
					console.log("error:"+error);
				});
	},
	destroyed(){
	},
	methods:{
		submitMpwd(){
			let data = new FormData();
			data.append("uid",this.mPwduser.uid);
			data.append("upwd",this.mPwduser.upwd);
			data.append("checkpwd",this.mPwduser.checkpwd);
			axios.post('/usert/modifyUpwd',data).then(resp=>{
				this.$message(resp.data);
				setTimeout(()=>{
					window.location.href="http://localhost:8082/";
				},2000);
				
				
			}).catch();
		},
		submitEmail(){
			axios.get('/usert/ifhasEmail',{
				params:{
					uemail:this.Email
				}
			}).then(resp=>{
				if(resp.data!==''){
					this.$message.error(resp.data);
				}
				else{
					let data = new FormData();
					data.append("uemail",this.Email);
					axios.post('/usert/findUpwdByUemail',data).then(resp=>{
						this.$message.success(resp.data.msg);
						console.log(resp.data);
					}).catch(error=>{
						console.log(error);
					});
				}
			}).catch(error=>{
				this.$message.error(error);
			});
			
		},
		isUid(text){
			if(text===''){
				//this.$message.error("用户昵称不能为空");
			}
		},
		getInterest(visible){
			if(visible===true){
			
		    }
		},
		process(percentage){
			return percentage===100?'已完成':'未完成';
		},
		nextBtn(index){
			if(index<2){
				this.divActive[index].div = false;
				this.divActive[index+1].div = true;
				this.percentage+=100/3;
			}
		},
		backBtn(index){
			if(index>0){
				this.divActive[index].div = false;
				this.divActive[index-1].div = true;
				this.percentage-=100/3;
			}
		},
		uploadUserImg(item){
			//console.log(item);
			this.imageUrl = URL.createObjectURL(item.file);//blobUrl
			console.log(this.imageUrl);
			this.ruleForm.uimg = item.file;
		},
        beforeAvatarUpload(file) {
	        const isJPG = file.type === 'image/jpeg';
	        const isLt2M = file.size / 1024 / 1024 < 2;
	
	        if (!isJPG) {
	          this.$message.error('上传头像图片只能是 JPG 格式!');
	        }
	        if (!isLt2M) {
	          this.$message.error('上传头像图片大小不能超过 2MB!');
	        }
	        return isJPG && isLt2M;
      },
		submitBtn(){
			this.percentage+=100/3;
			if(this.upwd!==this.upwd2)
			{
				this.$message.error("两次输入的密码不相同，请修改");
				return;
			}
			let data = new FormData();
			data.append("uid",this.ruleForm.uid);
			data.append("upwd",this.ruleForm.upwd);
			data.append("upasswordexpre",this.ruleForm.upasswordexpre);
			data.append("upasswordanswer",this.ruleForm.upasswordanswer);
			data.append("ugender",this.ruleForm.ugender);
			data.append("uemail",this.ruleForm.uemail);
			data.append("uphone",this.ruleForm.uphone);
			data.append("uinterest",this.ruleForm.uinterest);
			data.append("uimg",this.ruleForm.uimg);
			axios.post('/usert/addUsert',data)
				 .then((response)=>{
					this.$message.success("注册成功");
					let timeOut = setTimeout(()=>{
						window.location.href='http://localhost:8082/';
					},3000);
				}).catch(error=>{
					this.$message.error("注册失败，请重试");
					console.log("error:"+error);
				});
			
		}
	}
});